package com.yunhe.service.serviceimpl;

import com.yunhe.dao.daoimpl.CourseDaoImpl;
import com.yunhe.enity.Course;
import com.yunhe.service.CourseService;
import com.yunhe.utils.PageBeen;

import java.util.ArrayList;
import java.util.List;

public class CourseServiceImpl implements CourseService {
    CourseDaoImpl courseDao = new CourseDaoImpl();

    @Override
    public int addCourse(Course course) {

        return courseDao.addCourse(course);
    }

    @Override
    public PageBeen<Course> findPage(String currentPage, String pageSize, String courseName) {
        PageBeen<Course> pageBeen = new PageBeen<>();
        int cpage = Integer.parseInt(currentPage);
        int size = Integer.parseInt(pageSize);
        pageBeen.setCurrentPage(cpage);
        pageBeen.setPageSize(size);
        String sql = " select * from course ";
        ArrayList<Object> courses = new ArrayList<>();
        if (courseName != null && !"".equals(courseName.trim())) {
            sql += "  where courseName like ?  ";
            courses.add("%" + courseName + "%");
        }
        //总条数
        List<Course> courseList = courseDao.findCount(sql, courses);
        pageBeen.setTotalCount(courseList.size());
        //查询符合条件的数据
        List<Course> list = courseDao.findByLimit(sql, courses, cpage, size);
        pageBeen.setList(list);
        return pageBeen;
    }

    @Override
    public int delAll(String cid) {

        return courseDao.deleteAll(cid);
    }

    @Override
    public int updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    @Override
    public Course findCourse(Course course) {

        return courseDao.findCourByid(course);
    }
}
